package cn.rkylin.oms.base.resendReasonType.controller;

import cn.rkylin.core.controller.ApolloController;
import cn.rkylin.oms.base.refundReasonType.domain.RdfundReasonType;
import cn.rkylin.oms.base.refundReasonType.vo.RdfundReasonTypeVO;
import cn.rkylin.oms.base.resendReasonType.dao.ResendReasonTypeDAOImpl;
import cn.rkylin.oms.base.resendReasonType.domain.ResendReason;
import cn.rkylin.oms.base.resendReasonType.service.ResendReasonTypeService;
import cn.rkylin.oms.base.resendReasonType.vo.ResendReasonVO;
import cn.rkylin.oms.common.annotation.SystemControllerLog;
import cn.rkylin.oms.common.context.CurrentUser;
import cn.rkylin.oms.common.context.WebContextFactory;
import cn.rkylin.oms.common.dto.DBLog;
import cn.rkylin.oms.common.utils.LogUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.commons.lang.StringUtils;

/**
 * Created by Administrator on 2018-3-21.
 */
@Controller
@RequestMapping("/resendReasonType")
public class ResendReasonTypeController extends ApolloController {

    private static final String PAGE_SELECT_STATEMENT = "pageSelectResendReasonType";

    @Autowired
    private LogUtil logUtil;

    @Autowired
    public ResendReasonTypeService resendReasonTypeService;


    @SuppressWarnings({ "unchecked", "rawtypes" })
    @ResponseBody
    @RequestMapping(value = "/getResendReasonTypeList")
    public Map getResendReasonTypeList(String quickSearch, @RequestParam(required = false, defaultValue = "0") int start,
                                       @RequestParam(required = false, defaultValue = "10") int length) throws Exception {

        Map map = new HashMap();
        try {

            // 处理分页
            if (length == -1) {
                length = Integer.MAX_VALUE;
            }
            int page = start / length + 1;

            ResendReasonVO param = new ResendReasonVO();
            if (!StringUtils.isEmpty(quickSearch)) {
                quickSearch = URLDecoder.decode(quickSearch, "UTF-8");
                param.setResendReasonName(quickSearch);
            }

            // 处理转义的字段
            Map<String, String> replaceFieldsMap = new HashMap<String, String>();

            // 排序语句生成
            String orderStatement = getOrderString(ResendReasonTypeDAOImpl.class.getName(), PAGE_SELECT_STATEMENT, replaceFieldsMap);
            if (StringUtils.isNotEmpty(orderStatement)) {
                param.setOrderBy(orderStatement);
            }

            PageInfo<ResendReasonVO> resendReasonVOList = resendReasonTypeService.findByWhere(page, length, param);
            map.put(JSON_RESULT, SUCCESS);
            map.put("recordsFiltered", resendReasonVOList.getTotal());
            map.put("recordsTotal", resendReasonVOList.getTotal());
            map.put(RETURN_DATA, resendReasonVOList.getList());

        } catch (Exception ex) {
            ex.printStackTrace();
            map.put("result", "failed");
            map.put("msg", ex.getMessage());
        }

        return map;
    }

    @SuppressWarnings("rawtypes")
    @ResponseBody
    @RequestMapping(value = "/insertresendReasonType")
    @SystemControllerLog(module = "补发原因类型", operation = "插入补发原因类型", keyCode = "resendReasonId")
    public Map<String, Object> insertresendReasonType(@RequestBody ResendReasonVO resendReasonVO){
        Map<String, Object> map = new HashMap<String, Object>();
        if (resendReasonVO==null){
            map.put("result", "failed");
            map.put("msg", "没有要生成的数据");
            return map;
        }

        CurrentUser currentUser = WebContextFactory.getWebContext().getCurrentUser();
        if (currentUser==null){
            map.put("result", "failed");
            map.put("msg", "用户不存在");
            return map;
        }

        Iterator iterator = currentUser.getEnterpriseMap().keySet().iterator();
        String entId = (String) iterator.next();
        resendReasonVO.setEntId(entId);
        try {
            map = resendReasonTypeService.inserResendReason(resendReasonVO);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result", "failed");
            map.put("msg", e.getMessage());
            logUtil.error(new DBLog("", resendReasonVO.getResendReasonName(), "创建补发原因类型 ", resendReasonVO.toString()));
        }

        return map;
    }

    /**
     * 查询退款原因类型
     *
     * @param resendReasonId
     *            原因Id
     */
    @ResponseBody
    @RequestMapping(value = { "/getresendReasonTypeInfo/{resendReasonId}" })
    public Map<String, Object> getresendReasonTypeInfo(@PathVariable String resendReasonId) throws Exception {
        Map<String, Object> returnMap = new HashMap<String, Object>();
        try {
            ResendReasonVO vo = resendReasonTypeService.getResendReasonById(resendReasonId);
            if (vo != null) {
                returnMap.put(JSON_RESULT, SUCCESS);
                returnMap.put(RETURN_DATA, vo);
            } else {
                returnMap.put(JSON_RESULT, FAILED);
                returnMap.put(JSON_MSG, "该补发原因类型不存在");
            }
        } catch (Exception ex) {
            logUtil.error(ex.getMessage(), ex);
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(JSON_MSG, ex.getMessage());
        }

        return returnMap;
    }

    /**
     * 修改退款原因类型
     *
     * @param resendReasonVO
     *            退款原因类型
     */
    @ResponseBody
    @RequestMapping(value = "/updateresendReasonType")
    @SystemControllerLog(module = "补发原因类型", operation = "修改补发原因类型", keyCode = "resendReasonId")
    public Map<String, Object> updateresendReasonType(@RequestBody ResendReasonVO resendReasonVO) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (resendReasonVO==null){
            map.put("result", "failed");
            map.put("msg", "没有要生成的数据");
            return map;
        }

        try {
            map = resendReasonTypeService.updateResendReason(resendReasonVO);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result", "failed");
            map.put("msg", e.getMessage());
            logUtil.error(new DBLog("", resendReasonVO.getResendReasonName(), "修改补发原因类型 ", resendReasonVO.toString()));
        }

        return map;
    }

    /**
     * 删除退款原因类型
     *
     * @param resendReasonId
     *            原因Id
     */
    @ResponseBody
    @RequestMapping(value = { "/deleteresendReasonType/{resendReasonId}" })
    @SystemControllerLog(module = "补发原因类型", operation = "删除补发原因类型", keyCode = "resendReasonId")
    public Map<String, Object> deleteresendReasonType(@PathVariable String resendReasonId) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();

        try {
            map = resendReasonTypeService.deleteResendReasonById(resendReasonId);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result", "failed");
            map.put("msg", e.getMessage());
            logUtil.error(new DBLog("", resendReasonId, "删除补发原因类型 ", resendReasonId));
        }

        return map;
    }
}
